<?php
/**
 * Zend Framework Contribution
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Mwb
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    1.0 Schemata.php 3 mai 2012 Sergio DE VASCONCELOS
 */

require_once('Zend/Mwb/Dom/Abstract.php');
require_once('Zend/Mwb/Dom/Tables.php');
//require_once('Zend/Mwb/Dom/Views.php');

/**
 * MySQL Workbench document
 *
 * @category   Zend
 * @package    Zend_Mwb
 */
class Zend_Mwb_Dom_Schemata extends Zend_Mwb_Dom_Abstract
{
	protected $key = array(
		'name'   =>'Zend_Mwb_Dom_Type_String',
		'tables' =>'Zend_Mwb_Dom_Tables',
		'views'  =>'Zend_Mwb_Dom_Views',
	);
	
	protected $name;
	/**
	 * @var Zend_Mwb_Dom_Tables
	 */
	protected $tables;
	/**
	 * @var Zend_Mwb_Dom_Views
	 */
	protected $views;
	
	public function getName() {
		if (is_null($this->name)) {
			$this->name = $this->loadName();
		}
		return $this->name;
	}
	public function setName($name) {
		// TODO 
		 return $this;
	}
	/**
	 * return Mwb_Dom_Tables
	 */
	public function getTables() {
		if (is_null($this->tables)) {
			$this->name = $this->loadTables();
		}
		return $this->tables;
	}
	public function setTables($name) {
		// TODO 
		 return $this;
	}
	
	public function getViews() {
		if (is_null($this->views)) {
			$this->views = $this->loadViews();
		}
		return $this->views;
	}
	public function setViews($views) {
		// TODO 
		return $this;
	}
	
	
	public function parse() {
		$this->xpath = new DOMXPath($this->node->ownerDocument);
		
		$this->name   = $this->loadName();
		$this->tables = $this->loadTables();
		$this->view   = $this->loadViews();
	}
	/**
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function loadName() {
    	$element = $this->xpath->query("value[@key='name']", $this->node)->item(0);
        if (isset($element)) {
        	return $element->textContent;
        }// FIXME else throw Mwb_Dom_Exception
	}
	/**
	 * @param String $name
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function saveName($name) {
    	$element = $this->xpath->query("value[@key='name']", $this->node)->item(0);
        if (isset($element)) {
        	$element->textContent = $name;
        }// FIXME else create/lazyload
	}
	/**
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function loadTables() {
    	$elements = $this->xpath->query("value[@key='tables']/value", $this->node);
    	return new Zend_Mwb_Dom_Tables($elements, $this);
	}
	/**
	 * @param Mixed<Array|Mwb_Dom_Tables> $tables
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function saveTables($tables) {
        // TODO 
	}
	protected function loadViews() {
    	$element = $this->xpath->query("value[@key='views']/value", $this->node);
   		return new Zend_Mwb_Dom_Views($element, $this);
	}
	/**
	 * @param Mixed<Array|Mwb_Dom_Views> $tables
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function saveViews($views, DOMElement $dom=null) {
        // TODO 
	}
	
	public function create() {
$xml = <<<XXX
		<value type="object" struct-name="db.mysql.Schema" id="{747E97A7-B86F-4233-8EDC-E02644B9551B}" struct-checksum="0x20b94c22">
              <value _ptr_="0x5e00880" type="list" content-type="object" content-struct-name="db.mysql.RoutineGroup" key="routineGroups"/>
              <value _ptr_="0x5e00910" type="list" content-type="object" content-struct-name="db.mysql.Routine" key="routines"/>
              <value _ptr_="0x5e009a0" type="list" content-type="object" content-struct-name="db.mysql.Sequence" key="sequences"/>
              <value _ptr_="0x5e00a40" type="list" content-type="object" content-struct-name="db.mysql.StructuredDatatype" key="structuredTypes"/>
              <value _ptr_="0x5e00ad0" type="list" content-type="object" content-struct-name="db.mysql.Synonym" key="synonyms"/>
              <value _ptr_="0x5e00b60" type="list" content-type="object" content-struct-name="db.mysql.Table" key="tables">
              </value>
              <value _ptr_="0x5e00bf0" type="list" content-type="object" content-struct-name="db.mysql.View" key="views"/>
              <value type="string" key="defaultCharacterSetName"></value>
              <value type="string" key="defaultCollationName"></value>
              <value type="int" key="commentedOut">0</value>
              <value type="string" key="createDate">2008-10-29 11:22</value>
              <value _ptr_="0x5e006a0" type="dict" key="customData"/>
              <value type="string" key="lastChangeDate">2008-10-29 11:22</value>
              <value type="int" key="modelOnly">0</value>
              <value type="string" key="name">prestashop</value>
              <value type="string" key="temp_sql"></value>
              <value type="string" key="comment"></value>
              <value type="string" key="oldName">prestashop</value>
              <link type="object" struct-name="GrtObject" key="owner">{401C75FE-04D9-401C-BB75-A5A798C8EE71}</link>
            </value>
XXX;
	}
}
